package drds.datasource.connection_pool.impl.thread;

import drds.datasource.connection_pool.impl.DataSource;

public class DestroyConnectionThread extends Thread {


    private final DataSource dataSource;

    public DestroyConnectionThread(DataSource dataSource, String name) {
        super(name);
        this.dataSource = dataSource;
        this.setDaemon(true);
    }

    public void run() {
        this.dataSource.iniCountDownLatch.countDown();

        for (; ; ) {
            // 从前面开始删除
            try {
                if (this.dataSource.closed) {
                    break;
                }

                if (this.dataSource.timeBetweenEvictionRunsMillis > 0) {
                    Thread.sleep(this.dataSource.timeBetweenEvictionRunsMillis);
                } else {
                    Thread.sleep(1000); //
                }

                if (Thread.interrupted()) {
                    break;
                }

                this.dataSource.destroyConnectionTask.run();
            } catch (InterruptedException e) {
                break;
            }
        }
    }

}